package com.sunlee.admin.api.module.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunlee.admin.api.module.log.dao.SysLogOperationDao;
import com.sunlee.admin.api.module.log.dto.SysLogOperationDTO;
import com.sunlee.admin.api.module.log.entity.SysLogOperationEntity;
import com.sunlee.admin.api.module.log.service.SysLogOperationService;
import com.sunlee.common.core.constant.Constant;
import com.sunlee.common.core.entity.PageData;
import com.sunlee.common.core.service.impl.BaseServiceImpl;
import com.sunlee.common.core.utils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 操作日志
 *
 * @author sunlee
 */
@Service
public class SysLogOperationServiceImpl extends BaseServiceImpl<SysLogOperationDao, SysLogOperationEntity> implements SysLogOperationService {

    @Override
    public PageData<SysLogOperationDTO> page(Map<String, Object> params) {
        IPage<SysLogOperationEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );

        return getPageData(page, SysLogOperationDTO.class);
    }

    @Override
    public List<SysLogOperationDTO> list(Map<String, Object> params) {
        List<SysLogOperationEntity> entityList = baseDao.selectList(getWrapper(params));

        return ConvertUtils.sourceToTarget(entityList, SysLogOperationDTO.class);
    }

    private LambdaQueryWrapper<SysLogOperationEntity> getWrapper(Map<String, Object> params) {
        String status = (String) params.get("status");

        LambdaQueryWrapper<SysLogOperationEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(status), SysLogOperationEntity::getStatus, status);

        return wrapper;
    }
}